package com.github.od.handler;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.od.constants.CommonConstants;
import com.github.od.exception.BaseException;
import com.github.od.vo.BaseResponse;

/**
 * 异常拦截器
 * 
 * @author xieweikun
 * @version 2017年11月15日 下午8:32:38
 */
@ControllerAdvice("com.github.od")
@ResponseBody
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	@ExceptionHandler(BaseException.class)
	public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
		logger.error(ex.getMessage(),ex);
		return new BaseResponse(ex.getStatus(), ex.getMessage());
	}

	@ExceptionHandler(Exception.class)
	public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
		logger.error(ex.getMessage(),ex);
		return new BaseResponse(CommonConstants.EX_OTHER_CODE, ex.getMessage());
	}

}
